package com.xci.platform.web.aspect;

import com.xci.platform.annotation.QueryMap;
import com.xci.platform.helper.CoreHelper;
import com.xci.platform.helper.MapHelper;
import com.xci.platform.helper.ObjectHelper;
import com.xci.platform.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 查询参数拦截器
 */
@Aspect
@Slf4j
@Component
public class QueryMapAspect {

    @Before("@annotation(com.xci.platform.annotation.QueryMap)")
    public void doDataList(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        QueryMap mapPage = method.getAnnotation(QueryMap.class);
        String pageField = mapPage.page();
        String rowsField = mapPage.rows();
        String sortField = mapPage.sort();
        String orderField = mapPage.order();
        for (Object arg : args) {
            if (!(arg instanceof Map)) continue;
            try {
                Map<String, String> params = (Map<String, String>) arg;
                if (mapPage.trim()) { //去空格
                    MapHelper.trimMapValue(params);
                }
                if (params.containsKey(pageField) && params.containsKey(rowsField)) { //设置分页条件
                    CoreHelper.setPageParam(params.get(pageField), params.get(rowsField));
                }
                if (params.containsKey(sortField)) {
                    if (!params.get(sortField).contains(",")) { //单字段排序
                        params.put("_orderby", StringHelper.format("{} {}",
                                StringHelper.escapeSql(params.get(sortField)),
                                StringHelper.escapeSql(params.get(orderField))));
                        return;
                    }

                    //多字段排序
                    String[] sorts = StringHelper.splitToArray(params.get(sortField));
                    String[] orders = StringHelper.splitToArray(params.get(orderField));

                    if (ObjectHelper.isEmpty(sorts)) return;
                    if (ObjectHelper.isEmpty(orders)) {
                        orders = new String[sorts.length];
                        for (int i = 0; i < sorts.length; i++) {
                            orders[i] = "asc";
                        }
                    }
                    StringBuilder orderby = new StringBuilder();
                    for (int i = 0; i < sorts.length; i++) {
                        String sname = StringHelper.escapeSql(sorts[i]);
                        String sorder = StringHelper.escapeSql(orders[i]);
                        orderby.append(StringHelper.format("{} {}", sname, sorder));
                        if (i != sorts.length - 1) {
                            orderby.append(",");
                        }
                    }
                    params.put("_orderby", orderby.toString());
                }
            } catch (RuntimeException ex) {
                throw new RuntimeException("查询参数转换异常",ex);
            }
        }
    }
}
